home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_bas / tblevw.zip / TABLE.TXT < prev    next >
Text File  |  1994-04-29  |  6KB  |  245 lines

  1. Sub ExitApp ()
  2.  
  3.     ' Close database and table before exiting
  4.     Tb.Close
  5.     Db.Close
  6.     End
  7.  
  8. End Sub
  9.  
  10. Sub ExitMenuOption_Click ()
  11.  
  12.     Unload Me
  13.     
  14. End Sub
  15.  
  16. Sub FieldLayout ()
  17.  
  18.     ' Get Field Layout to determine field display
  19.     ' and data entry size
  20.     For ct = 0 To Tb.Fields.Count - 1
  21.         
  22.         'Set display heading to database fieldname
  23.         FldName = Tb.Fields(ct).Name
  24.         Table1.ColumnName(ct + 1) = FldName
  25.         
  26.         'Get width of fieldname
  27.         NameWidth = Len(FldName)
  28.  
  29.         'Get type of field to determine it's display size
  30.         Select Case Tb.Fields(ct).Type
  31.             Case 1, 10      'Text and Logic types
  32.                 FldSize = Tb.Fields(ct).Size
  33.             Case 3          'Integer type
  34.                 FldSize = 7
  35.             Case 4, 8       'Long and date types
  36.                 FldSize = 14
  37.             Case 5, 6, 7    'Currency, Single, Double types
  38.                 FldSize = 10
  39.             Case 11, 12     'Memo and binary types
  40.                 FldSize = 25
  41.         End Select
  42.  
  43.         ' Use field width or the field name width whichever is larger
  44.         If NameWidth > FldSize Then
  45.             Table1.ColumnWidth(ct + 1) = NameWidth + 2
  46.         Else
  47.             Table1.ColumnWidth(ct + 1) = FldSize + 2
  48.         End If
  49.  
  50.         ' Set data entry width to Field size
  51.         Table1.ColumnSize(ct + 1) = FldSize
  52.     Next ct
  53.  
  54. End Sub
  55.  
  56. Sub Form_Load ()
  57.  
  58.     ' Open Database and Table functions
  59.     OpenDb ("market.mdb")
  60.     OpenTb ("Contact_Info")
  61.     
  62.     ' Estimate begining size, put approx size in MAXROW
  63.     EndRow = MAXROW
  64.     ' Set grid Rows to estimated MAXROW
  65.     Table1.Rows = MAXROW
  66.     ' Set Current Row to one
  67.     Temp = MoveToRow(1)
  68.  
  69.     ' Function to setup grids columns
  70.     FieldLayout
  71.     
  72. End Sub
  73.  
  74. Sub Form_Resize ()
  75.  
  76.     ' Center the grid on the form
  77.     Table1.Top = TableForm.ScaleTop + 50
  78.     Table1.Left = TableForm.ScaleLeft + 50
  79.     Table1.Height = TableForm.ScaleHeight - 100
  80.     Table1.Width = TableForm.ScaleWidth - 100
  81.  
  82. End Sub
  83.  
  84. Sub Form_Unload (Cancel As Integer)
  85.  
  86.     ExitApp
  87.  
  88. End Sub
  89.  
  90. Function MoveToRow (NewRow As Long) As Long
  91.  
  92. Dim CurDiff, EndDiff, BeginDiff As Long
  93.  
  94.     ' Find differences between beginning, end and current position
  95.     CurDiff = Abs(CurrentRow - NewRow)
  96.     EndDiff = EndRow - NewRow
  97.     BeginDiff = NewRow - 1
  98.     
  99.     ' If values are same no need to move db
  100.     If CurrentRow = NewRow Then
  101.         MoveToRow = CurrentRow
  102.         Exit Function
  103.     
  104.     ' If moving forward in db
  105.     ElseIf CurrentRow < NewRow Then
  106.  
  107.         ' Check to see if End is closer, if not
  108.         ' move from current position to new position
  109.         If EndDiff > CurDiff Then
  110.             For ct = 1 To CurDiff
  111.                 Tb.MoveNext
  112.                 If Tb.EOF Then
  113.                     CurrentRow = Tb.RecordCount
  114.                     MoveToRow = CurrentRow
  115.                     Exit Function
  116.                 Else
  117.                     CurrentRow = CurrentRow + 1
  118.                 End If
  119.             Next ct
  120.         
  121.         ' If end is closer move to the end of the database
  122.         ' and go backwards to the new position
  123.         Else
  124.             Tb.MoveLast
  125.             CurrentRow = Tb.RecordCount
  126.             
  127.             'Check to see if estimated equal actual, if not equal
  128.             'exit function so CheckRows can set the actual EndRow value
  129.             If EndRow = Tb.RecordCount Then
  130.                 For ct = 1 To EndDiff
  131.                     Tb.MovePrevious
  132.                     CurrentRow = CurrentRow - 1
  133.                 Next ct
  134.             End If
  135.         End If
  136.     
  137.     ' Moving backward in db
  138.     Else
  139.  
  140.         ' If BeginDiff is greater than CurDiff then move
  141.         ' from current position to new position
  142.         If BeginDiff > CurDiff Then
  143.             For ct = 1 To CurDiff
  144.                 Tb.MovePrevious
  145.                 If Tb.BOF Then
  146.                     CurrentRow = 1
  147.                     MoveToRow = CurrentRow
  148.                     Exit Function
  149.                 Else
  150.                     CurrentRow = CurrentRow - 1
  151.                 End If
  152.             Next ct
  153.         
  154.         ' If beginning is closer then move from
  155.         ' beginning to new position
  156.         Else
  157.             Tb.MoveFirst
  158.             CurrentRow = 1
  159.             For ct = 1 To BeginDiff
  160.                 Tb.MoveNext
  161.                 CurrentRow = CurrentRow + 1
  162.             Next ct
  163.         End If
  164.     End If
  165.     MoveToRow = CurrentRow
  166.  
  167. End Function
  168.  
  169. Sub OpenDb (DbName As String)
  170.  
  171.     ' Put your open database code here
  172.     ChDir App.Path
  173.     Set Db = OpenDatabase(DbName)
  174.  
  175. End Sub
  176.  
  177. Sub OpenTb (TableName As String)
  178.  
  179.     ' Put your open table code here
  180.     Set Tb = Db.OpenTable(TableName)
  181.     
  182. End Sub
  183.  
  184. Sub Table1_CheckRows (RequestRows As Long, CurRows As Long)
  185.  
  186.     ' Move in table to value specified by RequestRows
  187.     NewRow = MoveToRow(RequestRows)
  188.     
  189.     ' If table did not make it to the NewRow value
  190.     ' i.e. NewRow was not attainable then
  191.     ' end of db was reached
  192.     If NewRow <> RequestRows Then
  193.         ' Set CurRows to actual end of file
  194.         CurRows = NewRow
  195.         ' Set EndRow to actual end of file
  196.         EndRow = NewRow
  197.     End If
  198.  
  199. End Sub
  200.  
  201. Sub Table1_Fetch (row As Long, Col As Integer, Value As String)
  202.  
  203.     ' This condition should always be true because of the
  204.     ' code in the CheckRows events but we double check
  205.     NewRow = MoveToRow(row)
  206. '    Debug.Print "OR=" & Str$(row)
  207. '    Debug.Print "NR =" & Str$(NewRow)
  208.  
  209.     If NewRow = row Then
  210.         
  211.         ' If field is empty trap Null and use empty quotes instead
  212.         If IsNull(Tb(Col - 1)) Then
  213.             Value = ""
  214.         Else
  215.             Value = Tb(Col - 1)
  216.         End If
  217.     Else
  218.         MsgBox "Error in navigating database"
  219.     End If
  220.     
  221. End Sub
  222.  
  223. Sub Table1_Update (row As Long, Col As Integer, Value As String)
  224.  
  225.     ' This should always be true because of the code in the
  226.     ' CheckRows but we double check anyways
  227.     If MoveToRow(row) = row Then
  228.         Call UpdateTable(Col, Value)
  229.     Else
  230.         MsgBox "Error updating value"
  231.     End If
  232.  
  233. End Sub
  234.  
  235. Sub UpdateTable (Column As Integer, NewValue As String)
  236.  
  237.         ' There is no error checking so becareful
  238.         ' of data mismatches!!!
  239.         Tb.Edit
  240.         Tb(Column - 1) = NewValue
  241.         Tb.Update
  242.     
  243. End Sub
  244.  
  245.